package com.qiyun.dinghuohui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qiyun.dinghuohui.MainActivity;
import com.qiyun.dinghuohui.R;
import com.qiyun.dinghuohui.activity.PairDetailActivity;
import com.qiyun.dinghuohui.base.BaseFragment;
import com.qiyun.dinghuohui.common.ClientCookie;
import com.qiyun.dinghuohui.common.CommonDataSets;
import com.qiyun.dinghuohui.common.ImageUtils;
import com.qiyun.dinghuohui.pop.ThSearchChangePop;
import com.qiyun.dinghuohui.utils.CommonUtil;
import com.qiyun.dinghuohui.utils.Inject;
import com.qiyun.dinghuohui.widget.DividerItemDecoration;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static com.qiyun.dinghuohui.common.CommonDataSets.getPairsByPairCode;
import static com.qiyun.dinghuohui.common.CommonDataSets.getPairsByPairgroup;

/**
 * Created by xlm on 2018\5\28 0028
 */

public class TogetherOrderFragment extends BaseFragment {

    @Inject(R.id.recyclerview)
    private RecyclerView mRecycleView;
    @Inject(R.id.filter)
    private LinearLayout mLinearSx;//筛选按钮

    @Inject(R.id.search)
    private RelativeLayout mRelaSearch;//搜索框部分
    @Inject(R.id.sort_choose)
    private LinearLayout mLinearSearchBtn;//选择搜索条件
    @Inject(R.id.edit_search)
    private AppCompatEditText mEditSearch;//搜索框
    @Inject(R.id.search_icon)
    private RelativeLayout mRelaSearchIcon;//搜索图标
    @Inject(R.id.edit_search_icon)
    private RelativeLayout mRelaSearchEditIcon;//搜索
    @Inject(R.id.search_option)
    private TextView mTxtSearch;

    private List<JSONObject> listGoods = new ArrayList<>();//商品信息

    private boolean isSearch = false;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.fragment_together);
    }

    @Override
    protected void initViews() {
        if (!isSearch) {
            mRelaSearchIcon.setVisibility(View.VISIBLE);
            mRelaSearch.setVisibility(View.GONE);
        }
        mEditSearch.setHint("按搭配号搜索");
        mTxtSearch.setText("搭配号");
        serchFilter = "搭配号";
    }

    @Override
    protected void onSetListener() {
        mRelaSearchEditIcon.setOnClickListener(getCurrentFragment());
        mLinearSx.setOnClickListener(getCurrentFragment());
        mLinearSearchBtn.setOnClickListener(getCurrentFragment());
        mRelaSearchIcon.setOnClickListener(getCurrentFragment());
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        listGoods = getPairsByPairgroup("");
        setTogetherAdapter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.filter:
                //切换drawer
                ((MainActivity) getActivity()).tooggleDrawer(getCurrentFragment());
                break;
            case R.id.sort_choose:
                showSearchPop();
                if (mSearchPop.getPopupWindow().isShowing()) {
                    mLinearSearchBtn.setSelected(true);
                }
                break;
            case R.id.search_icon:
                if (!isSearch) {
                    isSearch = true;
                    mRelaSearchIcon.setVisibility(View.GONE);
                    mRelaSearch.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.edit_search_icon:
                String text = mEditSearch.getText().toString();
                if (CommonUtil.isEmpty(text)) {
                    showToast("请输入" + serchFilter);
                } else {
                    //按照选择搜索
                    togetherAdapter.setNewData(getPairsByPairCode(text.toUpperCase()));
                    if (getPairsByPairCode(text.toUpperCase()).size() == 0) {
                        showToast("没有内容");
                    }
                    isSearch = false;
                    mEditSearch.setText("");
                    mRelaSearchIcon.setVisibility(View.VISIBLE);
                    mRelaSearch.setVisibility(View.GONE);
                }
                break;
        }
    }

    private ThSearchChangePop mSearchPop;
    private String serchFilter;

    private void showSearchPop() {
        if (mSearchPop == null) {
            mSearchPop = new ThSearchChangePop(getActivity());
            mSearchPop.setFocusable(true);
            mSearchPop.setListener(new ThSearchChangePop.Listener() {
                @Override
                public void onSelected(String filter) {
                    serchFilter = filter;
                    mEditSearch.setText("");
                    mEditSearch.setHint("按" + serchFilter + "搜索");
                    mTxtSearch.setText(serchFilter);
                }
            });
            mSearchPop.getPopupWindow().setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    hidePop();
                }
            });
        }
        mSearchPop.showAsDropDown(mRelaSearch);
    }

    private void hidePop() {
        mLinearSearchBtn.setSelected(false);
    }

    private TogetherAdapter togetherAdapter;

    private void setTogetherAdapter() {
        togetherAdapter = new TogetherAdapter(R.layout.item_together, listGoods);
        mRecycleView.setLayoutManager(new GridLayoutManager(getActivity(), 2, LinearLayoutManager.VERTICAL, false));
        mRecycleView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.GRID_LIST,
                getResources().getDrawable(R.drawable.shape_grid_div)));
        mRecycleView.setAdapter(togetherAdapter);
        togetherAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                PairDetailActivity.startActivity(getActivity(),listGoods.get(position));
            }
        });
    }

    //根据主界面回传的筛选信息，过滤商品
    public void filterOptions(String filter) {
        showLoading("加载中...");
        if (!"全部".equals(filter)) {
            listGoods = CommonDataSets.getPairsByPairgroup(filter);
        } else {
            listGoods = getPairsByPairgroup("");
        }
        togetherAdapter.setNewData(listGoods);
        mRecycleView.scrollToPosition(0);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissLoading();
            }
        }, 1200);
    }

    private class TogetherAdapter extends BaseQuickAdapter<JSONObject, BaseViewHolder> {

        public TogetherAdapter(int layoutResId, @Nullable List<JSONObject> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, JSONObject item) {
            int width = CommonUtil.getDisplayMetrics(getActivity()).widthPixels / 2;
//          helper.itemView.getLayoutParams().height = width;
            helper.itemView.getLayoutParams().width = width - 1;
            helper.setText(R.id.item_together_txt, item.optString("pair_id"));
//            String image_url = item.optString("image_url");
//            ImageUtils.showImage(getActivity(), (ImageView) helper.getView(R.id.item_together_image),
//                    ClientCookie.getServerUrl() + "/goods/img/" + "1504" + ".jpg");
            ImageUtils.showImage(getActivity(), (ImageView) helper.getView(R.id.item_together_image),
                    ImageUtils.imagUrl);
            int num = item.optInt("num", 0);
            if (num == 0) {
                helper.setImageResource(R.id.item_together_flag, R.mipmap.together_flag_none);
            } else if (num == 3) {
                helper.setImageResource(R.id.item_together_flag, R.mipmap.together_flag_all);
            } else {
                helper.setImageResource(R.id.item_together_flag, R.mipmap.together_flag_part);
            }
        }
    }

}
